package Reference;

import Moudle.Item;
import Utils.HttpConnect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import org.apache.http.ProtocolException;
import org.bson.Document;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static Map<String, String> header_map = new HashMap<>();
    static Map<String, String> data_map = new HashMap<>();
    static String url = "http://www.cninfo.com.cn/new/hisAnnouncement/query";
    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("巨潮网络");
    static MongoCollection mCollection = mDatabase.getCollection("沪市_会计差错");
    static int index = 1;

    static {
        data_map.put("pageNum", "1");
        data_map.put("pageSize", "30");
        data_map.put("column", "szse");
        data_map.put("tabName", "fulltext");
        data_map.put("plate", "sh");
        data_map.put("stock", "");
        data_map.put("searchkey", "会计差错");
        data_map.put("secid", "");
        data_map.put("category", "");
        data_map.put("trade", "");
        data_map.put("seDate", "2021-01-01~2021-05-20");
        data_map.put("sortName", "");
        data_map.put("sortType", "");
        data_map.put("isHLtitle", "true");


        header_map.put("Origin", "http://www.cninfo.com.cn");
        header_map.put("Referer", "http://www.cninfo.com.cn/new/commonUrl/pageOfSearch?url=disclosure/list/search&lastPage=index");
    }

    public static void main(String[] args) throws ProtocolException {
        TraverseEveryDay();
    }

    static void TraverseEveryDay() throws ProtocolException {

        int year = 2020;
        for (int month = 3; month <= 12; month++) {
            int endDay = 31;

            for (int day = 1; day <= endDay; day++) {
                String day_str = String.valueOf(day);
                String month_str = String.valueOf(month);
                if (day < 10)
                    day_str = "0" + day_str;
                if (month < 10)
                    month_str = "0" + month_str;
                String time = year + "-" + month_str + "-" + day_str + "~" + year + "-" + month_str + "-" + day_str;


                GetJsonStr(time, 1);
            }
        }


    }

    static void GetJsonStr(String time, int pageNum) throws ProtocolException {
        data_map.put("seDate", time);
        data_map.put("pageNum", String.valueOf(pageNum));

        String jsonStr = HttpConnect.HttpPost_OkHttp(url, header_map, data_map);
        JSONObject jsonObject = JSON.parseObject(jsonStr);

        try {
            if (jsonObject.getJSONObject("announcements") == null) {
                return;
            }
        } catch (ClassCastException e) {
            System.out.println(time + "  有数据");
        }


        JsonToDb(jsonObject, time, String.valueOf(pageNum));
        if (jsonObject.getString("hasMore").equals("true"))
            GetJsonStr(time, pageNum + 1);
    }

    static void JsonToDb(JSONObject JSONOBJECT, String time, String pageNum) throws ProtocolException {

        JSONArray announcements = JSONOBJECT.getJSONArray("announcements");
        List<Item> list = new ArrayList<>();
        int n = announcements.size();

        for (int i = 0; i < n; i++) {
            JSONObject jsonObject = announcements.getJSONObject(i);
            Item item = new Item(jsonObject.getString("adjunctSize"),
                    jsonObject.getString("secCode"),
                    jsonObject.getString("secName"),
                    jsonObject.getString("announcementId"),
                    jsonObject.getString("orgId"),
                    jsonObject.getString("announcementTime"),
                    jsonObject.getString("announcementTitle"),
                    "", ""
            );

            String title = item.announcementTitle.replace("*ST", "ST").replace(":", "：").replace("\"", "").replace("</em>", "").replace("<em>", "").replace("<", "（").replace(">", "）");
            String title_name = item.secCode + "_" + title + "_" + item.secName + "_" + item.announcementId + "_" + time + "_第" + pageNum + "页";
            item.title = title;
            item.title_name = title_name;
            list.add(item);
        }

        for (Item item : list) {
            Document doc = new Document("_id", item.announcementId)
                    .append("announcementId", item.announcementId)
                    .append("secName", item.secName)
                    .append("secCode", item.secCode)
                    .append("orgId", item.orgId)
                    .append("announcementTime", item.announcementTime)
                    .append("adjunctSize", item.adjunctSize)
                    .append("announcementTitle", item.announcementTitle)
                    .append("title", item.title)
                    .append("title_name", item.title_name);

            BasicDBObject obj = new BasicDBObject("$set", doc);
            BasicDBObject query = new BasicDBObject().append("_id", item.announcementId);
            mCollection.updateOne(query, obj, (new UpdateOptions()).upsert(true));
            System.out.println(doc.getString("announcementId") + " - " + doc.getString("secName") + " 写入数据库");
        }
    }

}
